Advanced Data Visualizations
13 March, 2024
You already….
Please install and load the following packages
Access lecture slide from the course landing page
I am Ayush.
I am a researcher working at the intersection of data, law, development and economics.
I teach Data Science using R at Gokhale Institute of Politics and Economics
I am a RStudio (Posit) certified tidyverse Instructor.
I am a Researcher at Oxford Poverty and Human development Initiative (OPHI), at the University of Oxford.
Reach me
ayush.ap58@gmail.com
ayush.patel@gipe.ac.in
ggplot2annotate() function can be used for any kind of geometric objectannotate() function, the type of geom is specified firstx and y, xmin and xmax is used for coordinates of the rectanglealpha is used for?annotate() over here requires x and xend coordinatesAuto data from ISLR2 packagehorsepower and accelerationHorsepower vs. Accelerationggplot2, the axes are mapped automatically based on the datascale functions in ggplot2:
wt and mpgscale_y_continuous() allows you to set the range for the y-axislimits inside the scale_y_continuous() provides limits of the scaleNA is used to refer to the existing maximumNA, if you had to provide 40 as the limit for y-axisbreaks in the scale_y_continuous allows you to set what intervals the axis will havescale_y_log10() does log transformation of the scalescale_colour_brewer() options are useful for plotting discrete values on your graphscale_colour_brewer helps in effcient mapping of discrete variablesAuto data, plot a scatterplot between y = weight and x = displacementscales package many scaling functions for visualizationstxhousing dataggplot2, the scales package has functions for breaks and labelsbreaks_width function provides a way to show every two years on the axis, while the label_date provides a way to show the last two digits of the year using %y, making it more clearcut_short_scale() function removes the additional 0 and supplements the K signtxhousing %>%
mutate(date = make_date(year, month, 1)) %>%
group_by(city) %>%
filter(min(sales) > 500) %>%
ggplot(aes(date, sales, group = city)) +
geom_line(na.rm = TRUE) +
scale_x_date(
NULL,
breaks = scales::breaks_width("2 years"),
labels = scales::label_date("'%y")) +
scale_y_log10(
"Total sales",
labels = scales::label_number(scale_cut = scales::cut_short_scale()))economics databreaks_width sets intervals for 3 monthslabel_date_short() does the task of shortening the date lengthsbreaks_extended()label_dollar() adds a dollar sign to the y-axiseconomics %>%
filter(date < ymd("1970-01-01")) %>%
ggplot(aes(date, pce)) +
geom_line() +
scale_x_date(NULL,
breaks = scales::breaks_width("3 months"),
labels = scales::label_date_short()) +
scale_y_continuous("Personal consumption expenditures",
breaks = scales::breaks_extended(8),
labels = scales::label_dollar())tourism data from openintropatchworkp1 <- economics %>%
filter(date < ymd("1970-01-01")) %>%
ggplot(aes(date, pce)) +
geom_line()
p2 <- economics %>%
filter(date < ymd("1970-01-01")) %>%
ggplot(aes(date, pce)) +
geom_line() +
scale_x_date(NULL,
breaks = scales::breaks_width("3 months"),
labels = scales::label_date_short())
p3 <- economics %>%
filter(date < ymd("1970-01-01")) %>%
ggplot(aes(date, pce)) +
geom_line() +
scale_x_date(NULL,
breaks = scales::breaks_width("3 months"),
labels = scales::label_date_short()) +
scale_y_continuous("Personal consumption expenditures",
breaks = scales::breaks_extended(8),
labels = scales::label_dollar())patchwork is very simple: you literally just add plots together!p1 and p2 show the intermediate steps, p3 is the final plottourism data, make line charts of year and visitor_count_tho, one for each decade